$(function () {
    var schoolcontact_datagrid = $('#schoolcontact_datagrid');
    var schoolcontact_dialog = $("#schoolcontact_dialog");
    var schoolcontact_form = $("#schoolcontact_form");
    var combobox_role = $("#combobox_role");
    var update_btn = $("#update_btn");
    $('#schoolcontact_datagrid').datagrid({
        url: '/schoolcontact/list',
        fit: true,
        pagination: true,
        rownumbers: true,
        border: false,
        singleSelect: true,
        fitColumns: true,
        toolbar: '#schoolcontact_datagrid_button',
        columns: [[
            {field: 'name', title: '姓名', width: 100},
            {field: 'male', title: '性别', width: 100,formatter: formatMale},
            {field: 'school', title: '学校', width: 100,formatter: formatSchool},
            {field: 'deptName', title: '学院名', width: 100},
            {field: 'duty', title: '职位', width: 100},
            {field: 'principal', title: '主联系人', width: 100,formatter: formatPrincipal},
            {field: 'tel', title: '电话', width: 100},
            {field: 'qq', title: 'QQ', width: 100},
            {field: 'email', title: '邮箱', width: 100},
        ]],
        onCheck:function(index,row) {
            console.log("99999999999999999999999999999");
            if (row.principal==2){
                update_btn.linkbutton({
                    disabled:true
                });
            }else{
                update_btn.linkbutton({
                    disabled:false
                });
            }
        }
    });

    var cmdObj = {
        searcch: function () {
            var keyword = $("#keyword").val();
            schoolcontact_datagrid.datagrid("load", {keyword: keyword});
        },
        addschoolContact: function () {
            schoolcontact_form.form("clear");
            schoolcontact_dialog.dialog("setTitle", "添加");
            schoolcontact_dialog.dialog("open");
        },
        updateSchoolContact: function () {
            var schoolcontact = schoolcontact_datagrid.datagrid("getSelected");
            if (!schoolcontact) {
                $.messager.alert('警告', '请选择你要编辑的一行', 'info');
                return;
            }
            schoolcontact_form.form("clear");
            $("#schoolcontact_form input").prop("disabled", false);
            //进行回显,针对联系人所在学校
            if (schoolcontact.school) {
                schoolcontact["school.id"] = schoolcontact.school.id;
            }
            schoolcontact_form.form("load", schoolcontact);
            schoolcontact_dialog.dialog("setTitle", "学校联系人编辑");
            schoolcontact_dialog.dialog("open");
        },
        //查看学校联系人checkSchoolContact
        checkSchoolContact: function () {
            var schoolcontact = schoolcontact_datagrid.datagrid("getSelected");
            if (!schoolcontact) {
                $.messager.alert('警告', '请选择你要编辑的一行', 'info');
                return;
            }
            schoolcontact_form.form("clear");
            //进行回显,针对联系人所在学校
            if (schoolcontact.school) {
                schoolcontact["school.id"] = schoolcontact.school.id;
            }
            schoolcontact_form.form("load", schoolcontact);
            schoolcontact_dialog.dialog("setTitle", "学校联系人编辑");
            $("#schoolcontact_form input").prop("disabled", true);
            schoolcontact_dialog.dialog("open");
        },
        fireSchoolContact: function () {
            var selectedRow = schoolcontact_datagrid.datagrid("getSelected");
            if (!selectedRow) {
                $.messager.alert('警告', '请选择你要删除的一行', 'warning');
                return;
            } else {
                $.messager.confirm('温馨提示', '你确定要删除该学校联系人？', function (yes) {
                    if (yes) {
                        $.post("/schoolcontact/delete", {id: selectedRow.id}, function (data) {
                            if (data.success) {
                                $.messager.alert('提示', "以后联系!", function () {
                                    schoolcontact_datagrid.datagrid("reload");
                                })
                            } else {
                                $.messager.alert('提示', data.msg, 'error');
                            }
                        })
                    }
                });
            }
        },
        reloadSchoolContact: function () {
            schoolcontact_datagrid.datagrid("reload");
        },
        save: function () {
            var url = "/schoolcontact/save";
            var urlId = $("#schoolcontact_id").val();
            if (urlId) {
                url = "schoolcontact/update"
            }
            schoolcontact_form.form('submit', {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('提示', "操作成功", 'info', function () {
                            cmdObj.cancel();
                            schoolcontact_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert('提示', data.msg, 'error')
                    }
                }
            });
        },
        cancel: function () {
            schoolcontact_dialog.dialog("close");
        }
    };
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });
});

function formatSchool(value,row,index){
    return value.schoolName;
}
function formatMale(value,row,index) {
    if (value == 1) {
        return "男";
    } else {
        return "女";
    }
}
function formatPrincipal(value,row,index){
    if(value==1){
        return "是";
    }else if(value==0){
        return "否";
    }else{
        return "<span style='color:grey'>"+"失效"+"</span>";
    }}
